package com.CampusLife.controller;

import  com.CampusLife.bean.LostProduct;
import com.CampusLife.bean.LostProductType;
import com.CampusLife.service.ILostProductTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.ILostProductService;
import com.CampusLife. until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/lostProduct")
public class LostProductController {
    @Resource
    private ILostProductService iLostProductService;
    @Resource
    private ILostProductTypeService iLostProductTypeService;
    @RequestMapping("/queryAll")
    public  String  findall(Model model){
        List<LostProduct> lostProducts=iLostProductService.getProductAll();
        model.addAttribute("lostProducts",lostProducts);
        List<LostProductType> list=iLostProductTypeService.queryAll();
        model.addAttribute("list",list);
        return  "admin/lostbasic";
    }
    @RequestMapping(path = "/addLostProduct", method = RequestMethod.POST)
    public String addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, HttpSession httpSession,ModelMap model, Integer l_tid, String lost_name, String describe_txt, String phone) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);

            DataTime dataTime=new DataTime();
            LostProduct lostProduct=new LostProduct();
            lostProduct.setLost_name(lost_name);
            lostProduct.setL_tid(l_tid);
            lostProduct.setDescribe_txt(describe_txt);
            lostProduct.setPhone(phone);
            lostProduct.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            lostProduct.setId(i);
            lostProduct.setPhoto(photo);
          iLostProductService.add(lostProduct);
        return "redirect:/lostProduct/";
    }
    //跳转到修改页面
    @RequestMapping("/jumpUpdateUserEdit")
    public String jumpUpdateUserEdit(Integer l_id,Model model){

        //数据回显
        LostProduct lostProduct=iLostProductService.queryById(l_id);
        model.addAttribute("lostProduct",lostProduct);
        return "jsp/update";
    }
    //修改·    /图片上传
    @RequestMapping(path = "/updateLostProduct", method = RequestMethod.POST)
    public String updataProduct(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, HttpSession httpSession,ModelMap model,Integer l_id ,Integer l_tid, String lost_name, String describe_txt, String phone) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
            DataTime dataTime=new DataTime();
            LostProduct lostProduct=new LostProduct();
            lostProduct.setL_id(l_id);
            lostProduct.setLost_name(lost_name);
            lostProduct.setL_tid(l_tid);

            lostProduct.setDescribe_txt(describe_txt);
            lostProduct.setPhone(phone);
            lostProduct.setTime(dataTime.gettime());
            int i=(int)httpSession.getAttribute("id");
            lostProduct.setId(i);
            lostProduct.setPhoto(photo);
            iLostProductService.update(l_id);

        return "jsp/cc";
    }
    @RequestMapping("/delete")
    public String delete(Integer l_id,Model model){
        iLostProductService.delete(l_id);
        return "redirect:/lostProduct/queryAll";
    }
      @RequestMapping("/ByName")
    public String Byname(String lost_name,Model model){
    List<LostProduct>   lostProducts= iLostProductService.selectByName(lost_name);
    model.addAttribute("lostProducts",lostProducts);
    return "";
    }
    @RequestMapping("/getProductAllByLostName")
    public String getProductAllByLostName(String lost_name,Model model) {
        LostProduct lostProduct = new LostProduct();
        lostProduct.setLost_name(lost_name);
        List<LostProduct> lostProducts = iLostProductService.getProductAllByLostName(lostProduct);
        model.addAttribute("lostProducts", lostProducts);
        List<LostProductType> list = iLostProductTypeService.queryAll();
        model.addAttribute("list", list);
        return "admin/lostbasic";
    }
    @RequestMapping("/getLostProductByType")
    public String getLostProductByType(Integer l_tid,Model model){
        List<LostProduct> lostProducts = iLostProductService.getLostProductByType(l_tid);
        model.addAttribute("lostProducts", lostProducts);
        List<LostProductType> list = iLostProductTypeService.queryAll();
        model.addAttribute("list", list);
        return "admin/lostbasic";
    }
}
